﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FlexButton">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/FlexButton;component/Themes/resXSButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate x:Key="stringOnlyTemplate">
        <Viewbox>
            <TextBlock Text="{Binding}" TextAlignment="Center"/>
        </Viewbox>
    </DataTemplate>

    <DataTemplate x:Key="defaultTextTemplate">
    </DataTemplate>

    <local:ButtonTextTemplateSelector
       DefaultTemplate="{StaticResource defaultTextTemplate}" 
       StringTemplate="{StaticResource stringOnlyTemplate}" 
       x:Key="buttonTextTemplateSelector" />

    <Style TargetType="{x:Type local:FlexButton}" >

        <!-- Provide DEFAULT values for some properties -->
        <Setter Property="CornerRadius" Value="4" />
        <Setter Property="Background" Value="{StaticResource brushClearGlass}" />
        <Setter Property="OuterBorderBrush" Value="{StaticResource brushOuterBorder}" />
        <Setter Property="OuterBorderThickness" Value="1" />
        <Setter Property="InnerBorderBrush" Value="{StaticResource brushInnerBorder}" />
        <Setter Property="InnerBorderThickness" Value="1" />
        <Setter Property="GlowColor" Value="{StaticResource brushGlow}" />
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="HighlightMargin" Value="1,1,0,0" />
        <Setter Property="HighlightBrightness" Value="184" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:FlexButton}">

                    <Border Name="OuterBorder" CornerRadius="{Binding CornerRadius, ElementName=InnerBorder}"
                            BorderBrush="{TemplateBinding OuterBorderBrush}" BorderThickness="{TemplateBinding OuterBorderThickness}" >

                        <Border Name="InnerBorder" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}"
                                BorderBrush="{TemplateBinding InnerBorderBrush}" BorderThickness="{TemplateBinding InnerBorderThickness}">

                            <Grid>
                                <Border Name="Glow" Opacity="0" CornerRadius="{Binding CornerRadius, ElementName=InnerBorder}">
                                    <Border.Background>
                                        <RadialGradientBrush>
                                            <GradientStop Offset="0" Color="{Binding GlowColor, RelativeSource={RelativeSource TemplatedParent},
                                                Converter={StaticResource ColorToAlphaColorConverter}, ConverterParameter=176}" />
                                            <GradientStop Offset="1" Color="{Binding GlowColor, RelativeSource={RelativeSource TemplatedParent},
                                                Converter={StaticResource ColorToAlphaColorConverter}, ConverterParameter=0}" />
                                        </RadialGradientBrush>
                                    </Border.Background>
                                </Border>

                                <Border Name="Highlight" Grid.Row="0" Margin="{Binding HighlightMargin, RelativeSource={RelativeSource Mode=FindAncestor,
                                    AncestorType={x:Type local:FlexButton}}}" CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource FindAncestor,
                                    AncestorLevel=1, AncestorType={x:Type Border}}, Converter={StaticResource HighlightCornerRadiusConverter}}" >
                                    <Border.Background>
                                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
                                            <GradientStop Offset="0" Color="{Binding HighlightBrightness,
                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}},
                                                Converter={StaticResource BrightnessToColorConverter}}" />
                                            <GradientStop Offset="0.8" Color="{Binding ButtonColor.Color,
                                                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}}}"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                </Border>

                                <Border Name="padding" Margin="{TemplateBinding Padding}"
                                        Visibility="{Binding IsHeaderInvisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}}}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>

                                        <Image HorizontalAlignment="Left" VerticalAlignment="Center" Name="TopImage" Grid.Column="0" Stretch="Uniform" StretchDirection="DownOnly" Source="{Binding ButtonImage, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}}}" Opacity="0.90">
                                            <Image.OpacityMask>
                                                <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                                                    <LinearGradientBrush.RelativeTransform>
                                                        <TransformGroup>
                                                            <ScaleTransform CenterY="0.5" CenterX="0.5"/>
                                                            <SkewTransform CenterY="0.5" CenterX="0.5"/>
                                                            <RotateTransform Angle="-90" CenterY="0.5" CenterX="0.5"/>
                                                            <TranslateTransform/>
                                                        </TransformGroup>
                                                    </LinearGradientBrush.RelativeTransform>
                                                    <GradientStop Color="Black" Offset="0.435"/>
                                                    <GradientStop Offset="1"/>
                                                </LinearGradientBrush>
                                            </Image.OpacityMask>
                                        </Image>

                                        <Grid Grid.Column="1" >
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="4*"/>
                                                <ColumnDefinition Width="92*"/>
                                                <ColumnDefinition Width="4*"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="5*"/>
                                                <RowDefinition Height="90*"/>
                                                <RowDefinition Height="5*"/>
                                            </Grid.RowDefinitions>
                                            <ContentPresenter Name="content" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"
                                                    ContentTemplateSelector="{StaticResource buttonTextTemplateSelector}">
                                            </ContentPresenter>
                                        </Grid>

                                    </Grid>
                                </Border>

                                <Border Margin="{TemplateBinding Padding}" Name="ImageBorder"
                                        Visibility="{Binding IsHeaderVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}}}">
                                    <Image Opacity="1" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Stretch="Uniform" StretchDirection="DownOnly" Name="TopImage2" Source="{Binding ButtonImage, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:FlexButton}}}"></Image>
                                </Border>
                            </Grid>
                        </Border>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="OuterBorder" Property="Opacity" Value="0.9" />
                            <Setter TargetName="InnerBorder" Property="Opacity" Value="0.9"/>
                            <Setter TargetName="content" Property="Margin" Value="3,1"/>
                            <Setter TargetName="Glow" Property="Opacity" Value="0.5" />
                            <Setter TargetName="Highlight" Property="Opacity" Value="0.5"/>
                            <Setter TargetName="TopImage2" Property="Margin" Value="3"/>
                            <Setter Property="ButtonColor" Value="Gray"/>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="Gray"/>
                        </Trigger>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource GlowOn}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource GlowOff}"/>
                            </Trigger.ExitActions>
                            <Setter TargetName="TopImage" Property="Opacity" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</ResourceDictionary>
